package com.example.service.impl;

import com.example.mapper.ClazzMapper;
import com.example.mapper.StudentMapper;
import com.example.params.ClazzQueryParam;
import com.example.pojo.Clazz;
import com.example.pojo.Emp;
import com.example.pojo.Student;
import com.example.service.ClazzService;
import com.example.vo.clazz.GetAllClazzInfoVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;
    public GetAllClazzInfoVo getAllClazz(ClazzQueryParam clazzQueryParam){
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> clazzList = clazzMapper.getAllClazz(clazzQueryParam);
        //获取当前时间格式化为yyyy-MM-dd
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义格式化模板
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化输出
        String formattedDate = currentDate.format(formatter);
        for (Clazz clazz : clazzList) {
            if(currentDate.isAfter(clazz.getEndDate())){
                clazz.setStatus("已结课");
            }
            else if(currentDate.isBefore(clazz.getBeginDate())){
                clazz.setStatus("未开班");
            }
            else{
                clazz.setStatus("已开班");
            }
        }

        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new GetAllClazzInfoVo(p.getTotal(),p.getResult());

    }

    public boolean deleteClazzById(Integer id){
        List<Student> students = studentMapper.findRelatedStudentByClass(id);
        if(students.size()>0) return false;
        return clazzMapper.deleteClazzById(id)>0;
    }

    public Clazz getClazzById(Integer id){
        return clazzMapper.getClazzById(id);
    }


    public boolean updateClazz(Clazz clazz) {

        return clazzMapper.updateClazz(clazz) > 0;
    }

    public boolean addClazz(Clazz clazz) {
        return clazzMapper.addClazz(clazz) > 0;
    }

    public List<Clazz> getAll(){
        return clazzMapper.getAll();
    }
}
